//
//  ZTRegex.m
//  ZHYYTool
//
//  Created by MT-SW-005 on 2021/9/17.
//  Copyright © 2021 MT-SW-005. All rights reserved.
//

#import "ZTRegex.h"

@implementation ZTRegex

+(NSArray*)matchesInString:(NSString*)sourceString expressionPattern:(NSString*)pattern
{
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray* array = [_regex matchesInString:sourceString options:0 range:NSMakeRange(0, [sourceString length])];
  
    NSMutableArray* resultArr = [[NSMutableArray alloc] init];
    for (NSTextCheckingResult *result in array)
    {
        NSString* strResult = [sourceString substringWithRange:result.range];
        
        [resultArr addObject:strResult];
        //NSLog(@"str is %@",strResult);
    }

    return [[NSArray alloc] initWithArray:resultArr];
}

+(BOOL)matchesInString:(NSString*)sourceString withString:(NSString*)strMatch
{
    BOOL flag = NO;
    NSString* pattern = [NSString stringWithFormat:@"\\b(%@)\\b",strMatch] ;
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSUInteger numberofMatches = [_regex numberOfMatchesInString:sourceString options:0 range:NSMakeRange(0, [sourceString length])];
    if (numberofMatches != NSNotFound && numberofMatches >0) {
        flag = YES;
    }
    return flag;
}


+(NSArray*)matchesInString:(NSString*)sourceString patternPrefix:(NSString*)prefix resultWithPrefix:(BOOL)flag
{
    NSString* pattern = @"";
    pattern = flag ? [NSString stringWithFormat:@"(?=%@)(\\w+)",prefix] : [NSString stringWithFormat:@"(?<=%@)\\w+",prefix];
    
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray* array = [_regex matchesInString:sourceString options:0 range:NSMakeRange(0, [sourceString length])];
    
    NSMutableArray* resultArr = [[NSMutableArray alloc] init];
    for (NSTextCheckingResult *result in array)
    {
        NSString* strResult = [sourceString substringWithRange:result.range];
        [resultArr addObject:strResult];
    }
    
    return [[NSArray alloc] initWithArray:resultArr];
}


+(NSArray*)matchesInString:(NSString*)sourceString fromString:(NSString*)strFrom toString:(NSString*)strTo
{
    NSString* pattern = [NSString stringWithFormat:@"(?<=%@).*?(?=%@)",strFrom,strTo];
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray* array = [_regex matchesInString:sourceString options:0 range:NSMakeRange(0, [sourceString length])];
    
    NSMutableArray* resultArr = [[NSMutableArray alloc] init];
    for (NSTextCheckingResult *result in array)
    {
        NSString* strResult = [sourceString substringWithRange:result.range];
        [resultArr addObject:strResult];
    }
    
    return [[NSArray alloc] initWithArray:resultArr];
}


+(NSArray*)matchesDecimalInString:(NSString*)sourceString
{
    NSString* pattern = @"-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|[1-9]\\d*)";
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray* array = [_regex matchesInString:sourceString options:0 range:NSMakeRange(0, [sourceString length])];
    
    NSMutableArray* resultArr = [[NSMutableArray alloc] init];
    for (NSTextCheckingResult *result in array)
    {
        NSString* strResult = [sourceString substringWithRange:result.range];
        [resultArr addObject:strResult];
    }
    
    return [[NSArray alloc] initWithArray:resultArr];
    //-?([1-9]\d+.\d+|0.\d*[1-9]\d*)
    //-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|[1-9]\d+)
    //-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|[1-9]\d*)
    
}



/**
* 用指定的字符串替换匹配到的字符串
*
@param sourceString 需要匹配替换的字符串
@param pattern 匹配规则
@param replaceString 替换的字符串
@return 返回替换后的字符串
*/
+(NSString*)replaceString:(NSString*)sourceString Pattern:(NSString*)pattern ReplaceString:(NSString*)replaceString
{
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *result = [_regex stringByReplacingMatchesInString: sourceString options:0 range:NSMakeRange(0, [sourceString length]) withTemplate:replaceString];
    return result;
}



/**
* 查找是否存在匹配的字符串
*
@param sourceString 需要匹配替换的字符串
@param pattern 匹配规则
@return 返回替换后的字符串
*/
+(NSString*)matchString:(NSString*)sourceString Pattern:(NSString*)pattern
{
    NSMutableString *str = [NSMutableString stringWithString:sourceString];
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSTextCheckingResult *chkResult = [_regex firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
    NSString *matchString = @"";
    if (chkResult) {
        matchString = [str substringWithRange:[chkResult rangeAtIndex:1]];
    }
    return matchString;
}


/**
* 查找匹配的字符串并返回匹配的所有的字符串
*
@param sourceString 需要匹配替换的字符串
@param pattern 匹配规则
@return 返回匹配的字符串
*/
+(NSArray*)mathchStrings:(NSString*)sourceString Pattern:(NSString*)pattern
{
    NSMutableString *str = [NSMutableString stringWithString:sourceString];
    NSRegularExpression *_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray *result = [_regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSTextCheckingResult *item in result) {
        [arr addObject:[str substringWithRange:item.range]];
    }
    return arr;
}

@end



